package com.ruoyi.ax.controller;

import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ax.domain.AxMySaleInfo;
import com.ruoyi.ax.domain.AxOrder;
import com.ruoyi.ax.service.IAxOrderService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ax.domain.AxUser;
import com.ruoyi.ax.service.IAxUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户充值信息Controller
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@RestController
@RequestMapping("/ax/axUser")
public class AxUserController extends BaseController
{
    @Autowired
    private IAxUserService axUserService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IAxOrderService iAxOrderService;

    @Autowired
    private RedisCache redisCache;


    /**
     * 查询用户充值信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AxUser axUser)
    {
        startPage();
        List<AxUser> list = axUserService.selectAxUserList(axUser);
        return getDataTable(list);
    }

    @GetMapping("/getInfo")
    public AjaxResult getInfo(AxUser axUser)
    {
          SysUser sysUser=new SysUser();
          Long  sysUserId= SecurityUtils.getUserId();
          sysUser=userService.selectUserById(sysUserId);
          AxMySaleInfo  axMySaleInfo=iAxOrderService.selectAxMySaleInfo(new AxOrder());
         // String estimatedAmount =String.format("%.2f", Double.valueOf(sysUser.getRemainRewardPoints()) / Double.valueOf("100"));
        String  estimatedAmount =String.format("%.2f", Double.valueOf(sysUser.getRemainRewardPoints()) + Double.valueOf(sysUser.getAbleCashIncome())
                + Double.valueOf(sysUser.getIndirectAbleCashIncome()));
          sysUser.setEstimatedAmount(estimatedAmount);
          BeanUtils.copyProperties(axMySaleInfo,sysUser);
          return success(sysUser);
    }

    /**
     * 导出用户充值信息列表
     */
    @Log(title = "用户充值信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AxUser axUser)
    {
        List<AxUser> list = axUserService.selectAxUserList(axUser);
        ExcelUtil<AxUser> util = new ExcelUtil<AxUser>(AxUser.class);
        util.exportExcel(response, list, "用户充值信息数据");
    }

    /**
     * 获取用户充值信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(axUserService.selectAxUserById(id));
    }


    @GetMapping(value = "/registSentMsg")
    public AjaxResult registSentMsg(SysUser sysUser)
    {
        if(StringUtils.isEmpty(sysUser.getPhonenumber())){
            return error("电话号码不能为空");
        }
        String regex = "^1[3-9]\\d{9}$";
        if (sysUser.getPhonenumber().matches(regex)) {

            if(redisCache.getCacheObject("time"+sysUser.getPhonenumber())!=null){
              long lastSendTime=  (long)redisCache.getCacheObject("time"+sysUser.getPhonenumber());
                if(((System.currentTimeMillis()-lastSendTime)/1000/60)<60){
                   // return error("发送间隙不能小于1分钟");
                }
            }

            Random r=new Random();
            int randomNum= (r.nextInt(9000)+1000);
            redisCache.setCacheObject(sysUser.getPhonenumber(),randomNum+"");
            redisCache.setCacheObject("time"+sysUser.getPhonenumber(),System.currentTimeMillis());
            try {
                MessageUtils.aliyunSendMsg(sysUser.getPhonenumber(),randomNum+"");
            } catch (Exception e) {
                e.printStackTrace();
                throw  new GlobalException("短信发送失败");
            }
        }else{
            return error("电话号码不合法");
        }
        // return success(axUserService.selectAxUserById(id));
        return  success(1);
    }

    /**
     * 新增用户充值信息
     */
    @Log(title = "用户充值信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AxUser axUser)
    {
        return toAjax(axUserService.insertAxUser(axUser));
    }

    /**
     * 修改用户充值信息
     */
    @Log(title = "用户充值信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AxUser axUser)
    {
        return toAjax(axUserService.updateAxUser(axUser));
    }

    /**
     * 删除用户充值信息
     */
    @Log(title = "用户充值信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(axUserService.deleteAxUserByIds(ids));
    }


    /**
     * 新增用户充值信息
     */
    @Log(title = "更新用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/appupdateSysuer")
    public AjaxResult appupdateSysuer(@RequestBody SysUser sysUser)
    {
        sysUser.setUserId(SecurityUtils.getUserId());
        return toAjax(userService.updateUser(sysUser));
    }

}
